//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.restaurant.application.util;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.DateTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.definitions.ButtonField;
import com.nebutown.description.data.SortSpec;
import com.nebutown.generation.basictype.UID;
import com.nebutown.generation.basictype.UIDLists;
import com.nebutown.supporthome.AbstractImpl;
import com.nebutown.supporthome.CmdCapsule;
import com.restaurant.application.coupon.Coupon;
import com.restaurant.application.coupon.support.CouponData;
import com.restaurant.application.couponmanage.support.CouponManageAndShopListData;
import com.restaurant.application.couponmanage.support.CouponManageData;
import com.restaurant.application.couponmanage.support.CreatePateTypeData;
import com.restaurant.application.indexes.couponlist.mysql.CouponListBean;
import com.restaurant.application.indexes.couponlist.mysql.CouponListCriteria;
import com.restaurant.application.indexes.couponlist.mysql.CouponListImpl;
import com.restaurant.application.indexes.couponmanagelist.mysql.CouponManageListBean;
import com.restaurant.application.indexes.couponmanagelist.mysql.CouponManageListCriteria;
import com.restaurant.application.indexes.couponmanagelist.mysql.CouponManageListImpl;
import com.restaurant.application.shoplist.ShopList;
import java.util.List;
import java.util.Objects;

public class CouponManageUtils {
    public CouponManageUtils() {
    }

    public static String getNumber(String type, AbstractImpl impl) {
        String number = type + DateTool.getCMNumber();
        List<CouponManageListBean> beans = ((CouponManageListImpl)impl.getIndex(CouponManageListImpl.class)).search((new CouponManageListCriteria()).withNumberLike(number), CommonUtils.getSort("RECORD_ID"), 1);
        number = number + (beans.size() > 0 ? ((CouponManageListBean)beans.get(0)).getRecordId() + 1L : 1L);
        return number;
    }

    public static UIDLists batchCreateCoupon(int amount, AbstractImpl impl, CouponManageData data, UID uid) {
        if ((long)amount < data.getTotalAmount() && (long)amount <= data.getTotalAmount() - data.getAmount()) {
            UIDLists uidLists = new UIDLists();

            for(int i = 0; i < amount; ++i) {
                uidLists.add(createCoupon(impl, data, uid));
            }

            return uidLists;
        } else {
            return null;
        }
    }

    public static UID createCoupon(AbstractImpl impl, CouponManageData data, UID uid) {
        UID customerUid = impl.getGate();
        CouponListCriteria criteria = (new CouponListCriteria()).withCustomerUidEquals(customerUid.value()).withCouponManageUidEquals(uid.value());
        int amount = ((CouponListImpl)impl.getIndex(CouponListImpl.class)).count(criteria);
        if (Objects.nonNull(data.getLimitId()) && (long)amount >= data.getLimitId()) {
            return null;
        } else {
            CouponListCriteria criteriaA = (new CouponListCriteria()).withCurrentCustomerUidEquals(customerUid.value()).withCouponManageUidEquals(uid.value());
            int totalAmount = ((CouponListImpl)impl.getIndex(CouponListImpl.class)).count(criteriaA);
            if (Objects.nonNull(data.getLimitAccount()) && (long)totalAmount >= data.getLimitAccount()) {
                return null;
            } else if (data.getTotalAmount() - data.getAmount() == 0L) {
                return null;
            } else if ("已过期".equals(data.getState())) {
                return null;
            } else {
                UID couponUid = impl.createNode(Coupon.class.getName()).getId();
                ((Coupon)impl.getUnit(Coupon.class, couponUid)).init(getCouponData(data, uid));
                return couponUid;
            }
        }
    }

    private static CouponData getCouponData(CouponManageData data, UID uid) {
        return (new CouponData()).withCouponManageUid(uid).withNumber(data.getNumber() + (data.getAmount() + 1L)).withEndTime(data.getEndTime()).withReceiveTime(data.getReceiveTime()).withType(data.getType()).withModality(data.getModality()).withFullMoney(data.getFullMoney()).withMinusMoney(data.getMinusMoney()).withRemarks(data.getRemarks()).withShopList(data.getShopList()).withShopNameList(data.getShopNameList()).withState(data.getState());
    }

    public static FormShell newFormShell(CreatePateTypeData typeData, DataModes dataModes, CouponManageData data, CmdCapsule cmd, CmdCapsule cancel, AbstractImpl impl, String type) {
        FormShell shell = new FormShell("优惠券/创建" + typeData.getType(), 2, 10);
        boolean editable = false;
        if (DataModes.EDIT.equals(dataModes)) {
            editable = true;
        }

        List<String> allShopName = CommonUtils.getAllShopName(impl);
        shell.source(data, dataModes);
        shell.at(0, 0, "类型").large(2, 1).stringField("type", type).placeHolder("类型").setEditable(false);
        shell.at(1, 0, "券名").large(2, 1).stringField("modality", data.getModality()).placeHolder("券名").notNull().setEditable(editable);
        if ("代金券".equals(type)) {
            shell.at(2, 0, "面额(元)").large(2, 1).doubleField("minusMoney", data.getMinusMoney()).placeHolder("面额(单位：元)").notNull().setEditable(editable);
        } else if ("满减券".equals(type)) {
            shell.at(2, 0, "满(元)").large(1, 1).doubleField("fullMoney", data.getFullMoney()).placeHolder("满(元)").notNull().setEditable(editable);
            shell.at(2, 1, "减(元)").large(1, 1).doubleField("minusMoney", data.getMinusMoney()).placeHolder("减(元)").notNull().setEditable(editable);
        } else if ("折扣券".equals(type)) {
            shell.at(2, 0, "折扣").large(2, 1).doubleField("minusMoney", data.getMinusMoney()).placeHolder("折扣").notNull().setEditable(editable);
        }

        shell.at(3, 0, "发行量").large(2, 1).longField("totalAmount", data.getTotalAmount()).placeHolder("发行量").notNull().setEditable(editable);
        shell.at(4, 0, "开始使用时间").large(1, 1).dateEntryField("receiveTime", data.getReceiveTime(), DateTypes.DATETIME).placeHolder("开始使用时间").notNull().setEditable(true);
        shell.at(5, 0, "截止使用时间").large(1, 1).dateEntryField("endTime", data.getEndTime(), DateTypes.DATETIME).placeHolder("截止使用时间").notNull().setEditable(true);
        shell.at(6, 0, "使用说明").large(2, 1).textArea("remarks", data.getRemarks(), 3L).placeHolder("使用说明").setEditable(true);
        if (allShopName.size() > 0) {
            shell.at(7, 0, "适用门店").large(2, 1).multipleChoice("shopNameList", allShopName).placeHolder("适用门店").notNull().setEditable(true);
        }

        shell.at(8, 0, "每个ID仅限领取(次)").large(2, 1).longField("limitId", data.getLimitId()).placeHolder("每个ID仅限领取(次)").setEditable(editable);
        shell.at(9, 0, "每个账户仅限(张)").large(2, 1).longField("limitAccount", data.getLimitAccount()).placeHolder("每个账户仅限(张)").setEditable(editable);
        List<ButtonField> fields = Lists.newArrayList();
        fields.add(new ButtonField(impl.getPath(cancel, new int[0]), "取消", true, (String)null, false));
        if (!Strings.isNullOrEmpty(impl.getPath(cmd, new int[0]))) {
            fields.add(new ButtonField(impl.getPath(cmd, new int[0]), "提交", true, (String)null, false));
        }

        shell.at(10, 0, (String)null).buttonBoxField((ButtonField[])fields.toArray(new ButtonField[0]));
        return shell;
    }

    public static void modifyCoupon(AbstractImpl impl, CouponManageData data, UID uid) {
        List<CouponListBean> beans = ((CouponListImpl)impl.getIndex(CouponListImpl.class)).search((new CouponListCriteria()).withCouponManageUidEquals(uid.value()), (SortSpec)null, 100000);
        if (beans.size() > 0) {
            CouponData couponData = new CouponData();
            if (Objects.nonNull(data.getEndTime()) && data.getEndTime() > 0L) {
                couponData.withEndTime(data.getEndTime());
            }

            if (data.getShopList().size() > 0) {
                couponData.withShopList(data.getShopList());
            }

            if (data.getShopNameList().size() > 0) {
                couponData.withShopNameList(data.getShopNameList());
            }

            if (!Strings.isNullOrEmpty(data.getRemarks())) {
                couponData.withRemarks(data.getRemarks());
            }

            beans.forEach((e) -> {
                ((Coupon)impl.getUnit(Coupon.class, e.getCouponUid())).update(couponData);
            });
        }

    }

    public static List<CouponManageAndShopListData> getShopListData(AbstractImpl impl, List<String> shopNameList) {
        List<CouponManageAndShopListData> shopListData = Lists.newArrayList();
        if (Objects.nonNull(shopNameList) && shopNameList.size() > 0) {
            shopNameList.forEach((e) -> {
                shopListData.add(getCouponManageAndShopListData(impl, e));
            });
        }

        return shopListData;
    }

    private static CouponManageAndShopListData getCouponManageAndShopListData(AbstractImpl impl, String shopName) {
        return (new CouponManageAndShopListData()).withShopName(shopName).withShopUid(((ShopList)impl.getSingleton(ShopList.class)).findShopByName(shopName));
    }
}
